/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package digitaldreamers.games.ddmonsters.servers.gameserver.data;

import digitaldreamers.libraries.games.servers.databasemanagement.concrete.DatabaseManagement;
import digitaldreamers.libraries.games.servers.databasemanagement.concrete.DatabaseManagement.DbmsType;
import digitaldreamers.libraries.games.servers.databasemanagement.concrete.SqlParameter;
import digitaldreamers.libraries.games.servers.databasemanagement.concrete.SqlParameter.ParameterDirection;
import digitaldreamers.libraries.games.servers.databasemanagement.concrete.SqlParameter.SqlDataType;
import digitaldreamers.games.ddmonsters.servers.gameserver.components.basicinfomation.PlayerInformation;
import digitaldreamers.games.ddmonsters.servers.gameserver.configurations.Configuration;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author DaoLe
 */
public class FacadeDataHandler {
    // <editor-fold defaultstate="collapsed" desc="Constants">
    private static final String SP_NAME__LOGIN_BY_LOGIN_AND_PASSWORD = "spLoginByLoginAndPassword";
    private static final String SP_NAME__LOGIN_BY_UIID = "spLoginByUiid";
    
    private static final String PARAMETER_NAME__LOGIN = "login";
    private static final String PARAMETER_NAME__PASSWORD = "password";
    private static final String PARAMETER_NAME__UIID = "uiid";
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="Static Members">
    private static final Object lockObj = new Object();
    private static FacadeDataHandler instance;
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="Members">
    private DatabaseManagement mDatabaseManagement;
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="Constructors">
    private FacadeDataHandler() {
        mDatabaseManagement = new DatabaseManagement(DbmsType.MySql, Configuration.DATABASE_SERVER_NAME, Configuration.DATABASE_SERVER_PORT, Configuration.DATABASE_NAME, Configuration.DATABASE_USERNAME, Configuration.DATABASE_PASSWORD, Configuration.DATABASE_TIMEOUT);
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="Methods">
    public static FacadeDataHandler getInstance() {
        if(instance == null) {
            synchronized(lockObj) {
                if(instance == null) {
                    instance = new FacadeDataHandler();
		}
            }
        }
        return instance;
    }
    
    public PlayerInformation getPlayerByUiid(String pUiid) throws SQLException {
        PlayerInformation playerInfo = null;
        SqlParameter[] parameters = new SqlParameter[1];
        parameters[0] = DatabaseManagement.createParameter(PARAMETER_NAME__UIID, ParameterDirection.Input, SqlDataType.VarChar, pUiid);
        ResultSet rs = mDatabaseManagement.executeQuerySP(SP_NAME__LOGIN_BY_UIID, parameters);
        if(rs.next()) {
            int playerId = rs.getInt(PlayerInformation.COLUMN__PLAYER_ID);
            String uiid = rs.getString(PlayerInformation.COLUMN__UIID);
            String login = rs.getString(PlayerInformation.COLUMN__LOGIN);
            String password = rs.getString(PlayerInformation.COLUMN__PASSWORD);
            String nickname = rs.getString(PlayerInformation.COLUMN__NICKNAME);
            String email = rs.getString(PlayerInformation.COLUMN__EMAIL);
            int starchip = rs.getInt(PlayerInformation.COLUMN__STARCHIP);
            int experience = rs.getInt(PlayerInformation.COLUMN__EXPERIENCE);
            int win = rs.getInt(PlayerInformation.COLUMN__WIN);
            int loss = rs.getInt(PlayerInformation.COLUMN__LOSS);
            playerInfo = new PlayerInformation(playerId, uiid, login, password, nickname, email, starchip, win, loss, experience);
        }
        return playerInfo;
    }
    
    public PlayerInformation getPlayerByLoginAndPassword(String pLogin, String pPassword) throws SQLException {
        PlayerInformation playerInfo = null;
        SqlParameter[] parameters = new SqlParameter[2];
        parameters[0] = DatabaseManagement.createParameter(PARAMETER_NAME__LOGIN, ParameterDirection.Input, SqlDataType.VarChar, pLogin);
        parameters[1] = DatabaseManagement.createParameter(PARAMETER_NAME__PASSWORD, ParameterDirection.Input, SqlDataType.VarChar, pPassword);
        ResultSet rs = mDatabaseManagement.executeQuerySP(SP_NAME__LOGIN_BY_LOGIN_AND_PASSWORD, parameters);
        if(rs.next()) {
            int playerId = rs.getInt(PlayerInformation.COLUMN__PLAYER_ID);
            String uiid = rs.getString(PlayerInformation.COLUMN__UIID);
            String login = rs.getString(PlayerInformation.COLUMN__LOGIN);
            String password = rs.getString(PlayerInformation.COLUMN__PASSWORD);
            String nickname = rs.getString(PlayerInformation.COLUMN__NICKNAME);
            String email = rs.getString(PlayerInformation.COLUMN__EMAIL);
            int starchip = rs.getInt(PlayerInformation.COLUMN__STARCHIP);
            int experience = rs.getInt(PlayerInformation.COLUMN__EXPERIENCE);
            int win = rs.getInt(PlayerInformation.COLUMN__WIN);
            int loss = rs.getInt(PlayerInformation.COLUMN__LOSS);
            playerInfo = new PlayerInformation(playerId, uiid, login, password, nickname, email, starchip, win, loss, experience);
        }
        return playerInfo;
    }
    // </editor-fold>
}
